###########################################################################
# $Id: conceptObjectItem.p 50 2009-07-15 08:54:51Z donbeave $
###########################################################################

@CLASS
conceptObjectItem

@USE
concept/models/conceptBaseItem.p

@BASE
conceptBaseItem

@OPTIONS
locals

#----- Constructor -----

@create[hshData;hshOptions]
  ^cleanMethodArgument[hshOptions]

  ^if(!def $hshOptions.classify){
    $hshOptions.classify[Objects]
  }

  ^BASE:create[$hshData;$hshOptions]

#----- Public -----

@factory[hshData;hshOptions]
  $result[^conceptObjectItem::create[$hshData;$hshOptions]]

@createChild[altType;altData;altLocale]
  $result[^STORAGE.createObject[$altType;$altData;$altLocale]]

# устанавливаем родителя для ребенка
  $result.parent[$self]

@hasParent[]
  $result(^ParentID.bool(false) || def $self._parent)

@hasChildren[]
  $result(^childrenCount.bool(false))

@hasData[altLocale]
  ^if($altLocale is string && def $altLocale){
    $altLocale[^STORAGE.getLocaleByLanguage[$altLocale]]
  }

  $result[^STORAGE.hasObjectLocalizedData[$self;$altLocale]]

@getData[altLocale]
  $result[^STORAGE.getObjectLocalizedData[$self;$altLocale]]

@setData[altData;objLocale]
  $result[^STORAGE.setLocalizedData[$self;$altData;$objLocale]]

@getChildByName[strName;objLocale]
  $oChilds[^getChildrenByName[$strName;$objLocale]]
  ^if($oChilds){
    $result[$oChilds.0]
  }

@getChildrenByName[strName;altLocale]
  ^if($altLocale is string && def $altLocale){
    $objLocale[^STORAGE.getLocaleByLanguage[$altLocale]]
  }

  $result[^STORAGE.getObjectChildrenByName[$self;$strName;$objLocale]]

@isBlocksLoaded[]
  $result[^STORAGE.isObjectBlocksLoaded[$ID]]

@save[]
  ^if(^isExist[]){
    $result(^DATA_MANAGER.editObject[$self])
  }{
     $result(^DATA_MANAGER.addObject[$self])

     ^if(def $self._parent){
       $self._ParentID($self._parent.ID)
     }

     $self._parent[]

#    устанавливаем значение, что объект существующий
     $self._isExist(true)
   }

#@update[]
#TODO возвращает состояние объекта, достает его оригинальные данные, заменяя внесенные изменения пользователя

@remove[]
  $result(^DATA_MANAGER.removeObject[$self])


#@deprecated
@getLocalizedData[objLocale]
  $result[^getData[$objLocale]]

#@deprecated
@setLocalizedData[altData;objLocale]
  $result[^setData[$altData;$objLocale]]

#----- Properties -----

@GET_DEFAULT[strName]
  $result[]

  ^if(^strName.match[^^\w{2}_\w{2}^$]){
    $result[^getData[$strName]]
  }

@GET_childrenCount[]
  $result(^self.ChildrenCount.int(0))

@GET_type[]
  $result[^STORAGE.getObjectTypeByID(^TypeID.int(0))]

@GET_parent[]
  ^if(def $self._parent){
    $result[$self._parent]
  }{
     $result[^STORAGE.getObjectParent[$self]]
   }

@SET_parent[objParent]
  ^labourAssert:required[objParent;conceptObjectItem]

  ^if(^objParent.isExist[]){
    ^pfAssert:isTrue(^objParent.ID.int(0) != $self.ParentID)[Object's parent is equal current parent.]

    $self._ParentID($objParent.ID)
  }{
     ^pfAssert:isTrue($objParent.UID ne $self._parent.UID)[Object's parent is equal current parent.]

     $self._parent[$objParent]
   }

  ^if(!($objParent._children is pfList)){
    $objParent._children[^pfList::create[]]
  }

  ^objParent._children.add[$self]

# увеличиваем значение количества потомков
  $objParent._ChildrenCount($objParent.childrenCount+1)

@GET_children[]
  ^if(^isExist[]){
    $result[^STORAGE.getObjectChildren[$self]]
  }{
     $result[$self._children]
   }

@GET_texts[]
  $result[^STORAGE.getObjectTexts[$self]]

@GET_localizedData[]
  $result[^getLocalizedData[$self]]

@GET_blocks[]
  $result[^STORAGE.getBlocksByObjectID[$ID]]

#----- Private -----

@_toHash[]
  $result[^BASE:_toHash[]]

  ^result.delete[ChildrenCount]
  ^result.delete[ChildsCount]

  $result.childrenCount($childrenCount)

  ^if(def $self._parent){
    $result.ParentUID[$self._parent.UID]
  }

  ^if(^isBlocksLoaded[]){
    $result.blocks[$blocks]
  }

  $result.texts[^pfList::create[]]

  ^texts.foreach[sLocaleID;oText]{
    ^result.texts.add[$oText]
  }